package utils;

import java.util.Dictionary;

public class IntConvert {
	private final static int getint(String str) {
		if (str != null) {
			str = str.trim();
			int v = 0;
			for (int i = 0; i < str.length(); i++) {
				char ch = str.charAt(i);
				if (ch >= '0' && ch <= '9') {
					v = v*10 + ch - '0';
				} else {
					return 0;
				}
			}
			return v;
		}
		return 0;
	}
	
	private final static long getlong(String str) {
		if (str != null) {
			str = str.trim();
			long v = 0;
			for (int i = 0; i < str.length(); i++) {
				char ch = str.charAt(i);
				if (ch >= '0' && ch <= '9') {
					v = v*10 + ch - '0';
				} else {
					return 0;
				}
			}
			return v;
		}
		return 0;
	}
	
	public final static int parseInt(Object obj) {
		if (obj != null) {
			if (obj instanceof String) {
				return getint((String) obj);
			} else if (obj instanceof Integer) {
				return ((Integer)obj).intValue();
			} else if (obj instanceof Long) {
				return ((Long)obj).intValue();
			}
		}
		return 0;
	}
	
	public final static long parseLong(Object obj) {
		if (obj != null) {
			if (obj instanceof String) {
				return getlong((String) obj);
			} else if (obj instanceof Integer) {
				return ((Integer)obj).intValue();
			} else if (obj instanceof Long) {
				return ((Long)obj).longValue();
			}
		}
		return 0;
	}
	
	public final static String toString(Object obj) {
		if (obj != null) {
			return obj.toString();
		}
		return null;
	}
	
	public final static void dictionaryPut(Dictionary<String, Object> dic, String key, Object value) {
		if (dic != null && key != null) {
			if (value == null) {
				dic.remove(key);
			} else {
				dic.put(key, value);
			}
		}
	}
	
	public final static String to2fString(String obj) {
		float v = 0;
		try {
			v = Float.parseFloat(obj);
		} catch (Exception e){
		}
		return String.format("%.2f", v);
	}
}
